#include "n1d_sessions.h"

LPSESSION gSessions;
uint32    gFirstFreeSession;
uint32    gMaxSessionNumber;

void n1d_sessions_initialize()
{
    gMaxSessionNumber = 100;
    gSessions = (LPSESSION)malloc(gMaxSessionNumber * sizeof(SESSION));
    gFirstFreeSession = 0;
}

int findFreeSession()
{
    for(uint32 i=0;i<gFirstFreeSession;i++)
    {
        if(!(gSessions+i)->is_used)
        {
            return i;
        }
    }
    if(gFirstFreeSession==gMaxSessionNumber) return -1;
    return gFirstFreeSession++;
}

LPSESSION getSession(int index)
{
    return (gSessions+index);
}

void      freeSession(int index)
{
    (gSessions+index)->is_used = 0;
    memset((gSessions+index), 0, sizeof(SESSION));
}
